# -*- coding: utf-8 -*-
__author__ = 'bobby'
import sys
import os


pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd+"../")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MxShop.MxShop.settings")

import django
django.setup()

from goods.models import Goods, GoodsCategory

from db_tools.data.product_data import row_data

for goods_detail in row_data:
    goods = Goods()
    goods.name = goods_detail["name"]
    goods.market_price = float(int(goods_detail["market_price"].replace("￥", "").replace("元", "")))
    goods.shop_price = float(int(goods_detail["sale_price"].replace("￥", "").replace("元", "")))
    goods.goods_brief = goods_detail["desc"] if goods_detail["desc"] is not None else ""
    goods.goods_desc = goods_detail["goods_desc"] if goods_detail["goods_desc"] is not None else ""
    goods.goods_front_image = goods_detail["images"][0] if goods_detail["images"] else ""

    goods_image = ""

    for index, item in enumerate(goods_detail["images"]):
        goods_image += item
        if index != len(goods_detail["images"]) - 1:
            goods_image += ","

    goods.goods_image = goods_image

    category_name = goods_detail["categorys"][-1]
    category = GoodsCategory.objects.filter(name=category_name)

    if category:
        goods.category = category[0]

    goods.save()
